'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _objectWithoutProperties2 = _interopRequireDefault(
  require('@babel/runtime/helpers/objectWithoutProperties'),
);
var _react = _interopRequireDefault(require('react'));
var _antd = require('antd');
var _difference = _interopRequireDefault(require('lodash/difference'));
var _createField = _interopRequireDefault(require('../../../createField'));
var _excluded = ['leftColumns', 'rightColumns', 'children', 'tableProps'];
var TableTransfer = function TableTransfer(_ref) {
  var leftColumns = _ref.leftColumns,
    rightColumns = _ref.rightColumns,
    children = _ref.children,
    _ref$tableProps = _ref.tableProps,
    tableProps = _ref$tableProps === void 0 ? {} : _ref$tableProps,
    rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  return /*#__PURE__*/ _react.default.createElement(
    _antd.Transfer,
    (0, _objectSpread2.default)({}, rest),
    function (_ref2) {
      var direction = _ref2.direction,
        filteredItems = _ref2.filteredItems,
        onItemSelectAll = _ref2.onItemSelectAll,
        onItemSelect = _ref2.onItemSelect,
        listSelectedKeys = _ref2.selectedKeys,
        listDisabled = _ref2.disabled;
      var columns = direction === 'left' ? leftColumns : rightColumns;
      var rowSelection = {
        getCheckboxProps: function getCheckboxProps(item) {
          return {
            disabled: listDisabled || item.disabled,
          };
        },
        onSelectAll: function onSelectAll(selected, selectedRows) {
          var treeSelectedKeys = selectedRows
            .filter(function (item) {
              return !item.disabled;
            })
            .map(function (_ref3) {
              var key = _ref3.key;
              return key;
            });
          var diffKeys = selected
            ? (0, _difference.default)(treeSelectedKeys, listSelectedKeys)
            : (0, _difference.default)(listSelectedKeys, treeSelectedKeys);
          onItemSelectAll(diffKeys, selected);
        },
        onSelect: function onSelect(_ref4, selected) {
          var key = _ref4.key;
          onItemSelect(key, selected);
        },
        selectedRowKeys: listSelectedKeys,
      };
      return /*#__PURE__*/ _react.default.createElement(
        _antd.Table,
        (0, _objectSpread2.default)(
          (0, _objectSpread2.default)(
            {
              rowSelection: rowSelection,
              dataSource: filteredItems,
              size: 'small',
              // @ts-ignore
              style: {
                pointerEvents: listDisabled ? 'none' : null,
              },
            },
            tableProps,
          ),
          {},
          {
            // @ts-ignore
            columns: columns,
            // @ts-ignore
            onRow: function onRow(_ref5) {
              var key = _ref5.key,
                itemDisabled = _ref5.disabled;
              return {
                onClick: function onClick() {
                  if (itemDisabled || listDisabled) return;
                  onItemSelect(key, !listSelectedKeys.includes(key));
                },
              };
            },
          },
        ),
      );
    },
  );
};
var _default = (0, _createField.default)(TableTransfer, {
  defaultFormItemProps: {
    valuePropName: 'targetKeys',
  },
});
exports.default = _default;
